package com.spring.demo10.eg3;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @author vhans
 */
public class Test {
    @org.junit.Test
    public void test() throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getBeanFactory().registerScope(BeanRefreshScope.SCOPE_REFRESH, BeanRefreshScope.getInstance());

        context.register(MainConfig3.class);
        // 刷新mail的配置到Environment
        RefreshConfigUtil.refreshMailPropertySource(context);
        context.refresh();

        MailService mailService = context.getBean(MailService.class);

        System.out.println("不更新配置，输出3次");
        for (int i = 0; i < 3; i++) {
            System.out.println(mailService);
            TimeUnit.MILLISECONDS.sleep(800);
        }

        System.out.println("更新配置，输出3次");
        for (int i = 0; i < 3; i++) {
            RefreshConfigUtil.updateDbConfig(context);
            System.out.println(mailService);
            TimeUnit.MILLISECONDS.sleep(800);
        }
    }
}
